package com.loop.unclelend.utils.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UncleDialogBindCardSubmitBinding
import com.loop.unclelend.pro.ProDialog
import com.loop.unclelend.utils.DensityUtils
import com.loop.unclelend.utils.ImageUtil
import com.loop.unclelend.utils.adapter.UncleOnItemChildClickListener
import com.loop.unclelend.utils.adapter.UncleRecyclerViewAdapter
import com.loop.unclelend.utils.adapter.UncleViewHolderHelper
import com.loop.unclelend.utils.decoration.LinearSpacingItemDecoration
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/20 13:45
 * @Annotation：绑卡确认dialog
 */
class Dialog_BindCard_Submit: ProDialog {
    private lateinit var binding: UncleDialogBindCardSubmitBinding
    private var mCallback: IDialogCallback? = null

    private var itemAdapter: ItemAdapter? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = UncleDialogBindCardSubmitBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        initRecyclerView()
    }

    override fun initListener() {
        setOnClickListener(binding.btnConfirm)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if(mCallback != null){
            mCallback!!.onClick(v)
        }
    }

    /**
     * 初始化账户列表
     */
    private fun initRecyclerView() {
        val topDecoration = DensityUtils.dp2px(14f)
        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0, 0, topDecoration, 0))
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        itemAdapter = ItemAdapter(binding.recyclerView)
        binding.recyclerView.adapter = itemAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        itemAdapter!!.setOnItemChildClickListener(UncleOnItemChildClickListener { _, childView, position ->
        })
    }

    /**
     * 设置账户列表
     */
    fun setBindAccountList(accountList: MutableList<JSONObject>){
        itemAdapter!!.data = accountList
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    class ItemAdapter(recyclerView: RecyclerView?) : UncleRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.uncle_item_account) {
        override fun setItemChildListener(helper: UncleViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.layout_item) //点击Item
        }
        override fun fillData(helper: UncleViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.txt_title,model.optString("walletDesc"))
            helper.setText(R.id.txt_account,"+" + model.optString("walletMobile"))
            val imgLogo = helper.getImageView(R.id.img_logo)
            ImageUtil.instance!!.loadImageUrl(mContext,imgLogo,model.optString("walletLogo"), R.mipmap.icon_holder)

            //不显示删除
            helper.setVisibility(R.id.img_delete,View.INVISIBLE)
        }
    }

    override fun onStart() {
        super.onStart()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.DIALOG.UNCLE_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.DIALOG.UNCLE_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_DISMISS)
    }
}